<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>社保稽查 注册</title>
    <c:set var="path" value="${pageContext.request.contextPath}"/>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="${path}/plugins/assets/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="${path}/plugins/assets/css/style.css">
    <link rel="stylesheet" href="${path}/plugins/assets/font-awesome/css/font-awesome.min.css">
    <script type="text/javascript" src="${path}/plugins/bootstrap/js/jquery.min.js"></script>
    <script type="text/javascript" src="${path}/plugins/assets/bootstrap/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="${path}/plugins/assets/js/jquery.backstretch.min.js"></script>
    <script type="text/javascript" src="${path}/plugins/assets/js/jquery.validate.min.js"></script>
    <script type="text/javascript">
        function changeImage() {
            $("#captchaImage").prop("src", "${path}/User/imgCode?time=" + new Date().getTime());
        }

        var countdown = 60;

        function setTime(obj) {
            if (countdown == 0) {
                obj.removeAttribute("disabled");
                obj.value = "获取验证码";
                countdown = 60;
                return;
            } else {
                obj.setAttribute("disabled", true);
                obj.value = "重新发送(" + countdown + ")";
                countdown--;
            }
            setTimeout(function () {
                setTime(obj);
            }, 1000);
        }

        function code(obj) {
            let phone = $("#phone").val();

            if (phone == "" || phone == null) {
                alert("请输入手机号");
                return;
            }
            if (!(/^1(3|5|6|8)\d{9}$/.test(phone))) {
                alert("手机号码格式有误，请重填");
                return;
            }
            setTime(obj);
            $.ajax({
                url: "${path}/User/code",
                type: "post",
                data: "phone=" + phone,
                dataType: "json",
                success: function (status) {
                    console.log(status);
                    if (status == "发送成功") {
                        var text = $("#text").val();
                        if (text != null) {
                            $("#text").html("");
                        }
                        $("#texts").html(status);
                    } else {
                        var text = $("#texts").val();
                        if (text != null) {
                            $("#texts").html("");
                        }
                        $("#text").html(status);
                    }
                }
            })
        }

        function insert() {

            let username = $("#form-username").val();
            let realName = $("#form-realName").val();
            let password = $("#form-password").val();
            let imgCode = $("#ImgCode").val();
            let phone = $("#phone").val();
            let phoneCode = $("#phoneCode").val();

            if (username == "" || username == null) {
                alert("请输入用户名");
                return;
            }
            if (realName == "" || realName == null) {
                alert("请输入姓名");
                return;
            }
            if (!(/^[\u4e00-\u9fa5]{0,10}$/.test(realName))) {
                alert("请正确输入姓名");
                return;
            }
            if (!(/^[a-zA-Z]\w{3,15}$/.test(username))) {
                alert("请输入4到16位(字母,数字,下划线,减号)用户名");
                return;
            }

            if (password == "" || password == null) {
                alert("请输入密码");
                return;
            }
            if (!(/^\w{5,17}$/.test(password))) {
                alert("密码至少6位");
                return;
            }
            if (phone == "" || phone == null) {
                alert("请输入手机号");
                return;
            }
            if (!(/^1(3|5|6|8)\d{9}$/.test(phone))) {
                alert("手机号码格式有误，请重填");
                return;
            }
            if (phoneCode == "" || phoneCode == null) {
                alert("请输入手机验证码");
                return;
            }
            if (imgCode == "" || imgCode == null) {
                alert("请输入字符验证码");
                return;
            }
            var datas = {
                "username": username,
                "realName": realName,
                "password": password,
                "ImgCode": imgCode,
                "phone": phone,
                "phoneCode": phoneCode
            };
            $.ajax({
                url: "${path}/User/insert",
                type: "post",
                data: datas,
                dataType: "json",
                success: function (status) {
                    if (status == "true") {
                        var text = $("#text").val();
                        if (text != null) {
                            $("#text").html("");
                        }
                        $("#texts").html("注册成功");
                        location.href = "${path}/login.jsp";
                    } else {
                        var texts = $("#texts").val();
                        if (texts != null) {
                            $("#texts").html("");
                        }
                        $("#text").html(status);
                    }
                    if (status == "用户名已存在...") {
                        $("#form-username").val('');
                    } else if (status == "验证码错误...") {
                        $("#ImgCode").val('');
                        changeImage();
                    } else if (status == "手机验证码错误...") {
                        $("#phoneCode").val("");
                    }
                }
            })
        }
    </script>
</head>
<body style="background: url(${path}/plugins/assets/img/backgrounds/235.jpg)">
<div class="top-content">
    <div class="inner-bg">
        <div class="container">
            <div class="row">
                <div class="text text-center">
                    <h1><strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;稽查平台</strong></h1>
                    <br/><br/>
                </div>
                <div class="form-top col-lg-offset-4" style="width: 450px">
                    <div class="form-top-left">
                        <h3>后台管理注册</h3>
                        <p>请输入账户,密码然后摁下注册键</p>
                    </div>
                    <div class="form-top-right">
                        <i class="fa fa-key"></i>
                    </div>
                </div>
                <div class="form-bottom col-lg-offset-4" style="width: 450px">
                    <form role="form" action="" method="post" class="login-form" id="loginForm">
                        <span class="text" id="text" style="color: red"></span>
                        <span class="texts" id="texts" style="color: #00ca6d"></span>
                        <span id="msgDiv"></span>
                        <div class="pull-right">
                            <a class="btn-xs btn-link" href="${path}/login.jsp">已有账户? 去登陆</a>
                        </div>
                        <br/>
                        <%--用户名开始--%>
                        <div class="form-group">
                            <label class="sr-only" for="form-username">Username</label>
                            <input type="text" name="username" placeholder="请输入用户名..."
                                   class="form-username form-control" required id="form-username">
                        </div>
                        <%--姓名开始--%>
                        <div class="form-group">
                            <label class="sr-only" for="form-realName">Realname</label>
                            <input type="text" name="username" placeholder="请输入姓名..."
                                   class="form-username form-control" required id="form-realName">
                        </div>
                        <%--密码开始--%>
                        <div class="form-group">
                            <label class="sr-only" for="form-password">Password</label>
                            <input type="password" name="password" placeholder="请输入密码..."
                                   class="form-password form-control" required id="form-password">
                        </div>
                        <%--手机号开始--%>
                        <div class="form-group form-inline">
                            <label class="sr-only" for="exampleInputPhone">Phone</label>
                            <input type="text" name="phone" placeholder="请输入手机号..." style="width:250px" id="phone"
                                   class="form-password form-control" required id="exampleInputPhone">
                            <label class="sr-only" for="phoneCode">PhoneCode</label>
                            <input type="text" style="width:144px" class="form-control form-password" required
                                   id="phoneCode" name="phoneCode" placeholder="请输入手机验证码...">
                            <input onclick="code(this);" class="btn btn-info btn-xs" type="button" id="phoneCodes"
                                   value="获取验证码"/>
                            <div class="form-group"></div>
                        </div>
                        <%--验证码开始--%>
                        <div class="form-group">
                            <div class="form-group">
                                <img id="captchaImage" style="height: 32px;float: left" onclick="changeImage()"
                                     class="captchaImage" src="${path}/User/imgCode" width="80px" height="18px"/>
                                <label class="sr-only" for="form-password">ImgCode</label>
                                <input style="width: 300px;float: left;margin-left: 20px" type="text" name="ImgCode"
                                       placeholder="请输入验证码..." id="ImgCode"
                                       class="form-password form-control" required id="form-ImgCode"><br/>
                            </div>
                        </div>
                        <input type="button" onclick="insert()"
                               style="width: 400px;border:1px solid #9d9d9d;border-radius: 4px; color: #010101"
                               id="loginButtonId" value="注册">
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
